package com.dreamwhale.machinery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.dao.PriceRangeMapper;
import com.dreamwhale.machinery.entity.PriceRange;
import com.dreamwhale.machinery.entity.YxStoreProduct;
import com.dreamwhale.machinery.goods.entity.YxStoreCategory;
import com.dreamwhale.machinery.goods.mapper.YxStoreCategoryMapper;
import com.dreamwhale.machinery.goods.mapper.YxStoreProductMapper;
import com.dreamwhale.machinery.service.YxStoreProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-05-30
 */
@Service
public class YxStoreProductServiceImpl extends ServiceImpl<YxStoreProductMapper, YxStoreProduct> implements YxStoreProductService {

    @Resource
    private YxStoreProductMapper yxStoreProductMapper;

    @Resource
    private PriceRangeMapper priceRangeMapper;

    @Resource
    private YxStoreCategoryMapper yxStoreCategoryMapper;

    @Override
    public List<YxStoreProduct> selectYxStoreProductSaleAsc(Integer type, Integer types, Integer brandId, Integer cateId, Integer priceId, String storeName) {
        QueryWrapper<YxStoreProduct> queryWrapper = new QueryWrapper<>();
        if (type != null) {
            if (type == 1) {
                queryWrapper.orderByDesc("sales");

            } else {
                queryWrapper.orderByAsc("sales");
            }
        }
        if (types != null) {
            if (types == 1) {
                queryWrapper.orderByDesc("price");

            } else {
                queryWrapper.orderByAsc("price");
            }
        }
        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);

        }
        if (storeName != null) {
            queryWrapper.like("store_name", storeName);

        }
        if (priceId != null) {
            QueryWrapper<PriceRange> price = new QueryWrapper<>();
            price.eq("price_range_id", priceId);
            List<PriceRange> priceRanges = priceRangeMapper.selectList(price);
            queryWrapper.between("price", priceRanges.get(0).getPriceRangeMin(), priceRanges.get(0).getPriceRangeMax());

        }

        queryWrapper.eq("cate_id", cateId).eq("is_del", 0).eq("is_show", 1);
        return yxStoreProductMapper.selectList(queryWrapper);
    }

    @Override
    public YxStoreProduct selectYxStoreProductById(String storeProductId) {
        return yxStoreProductMapper.selectById(storeProductId);
    }

    @Override
    public List<YxStoreProduct> selectYxStoreProductCount(Integer merId) {
        QueryWrapper<YxStoreProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mer_id", merId).eq("is_del", 0).eq("is_show", 1);

        return yxStoreProductMapper.selectList(queryWrapper);
    }

    @Override
    public List<YxStoreProduct> selectYxStoreProductByMerId(Integer merId, Integer type, Integer types, Integer cateId, Integer brandId) {
        QueryWrapper<YxStoreProduct> queryWrapper = new QueryWrapper<>();
        if (type != null) {
            if (type == 1) {
                queryWrapper.orderByDesc("sales");

            } else {
                queryWrapper.orderByAsc("sales");
            }
        }
        if (types != null) {
            if (types == 1) {
                queryWrapper.orderByDesc("price");

            } else {
                queryWrapper.orderByAsc("price");
            }
        }
        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);

        }
        if (cateId != null) {
            queryWrapper.eq("cate_id", cateId);
        }
        queryWrapper.eq("mer_id", merId).eq("is_del", 0).eq("is_show", 1);

        return yxStoreProductMapper.selectList(queryWrapper);
    }

    @Override
    public List<YxStoreProduct> selectYxStoreProductNews(Integer brandId, Integer cateId, Integer priceId, String storeName) {
        QueryWrapper<YxStoreProduct> queryWrapper = new QueryWrapper<>();
        //品牌
        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);

        }
        //名称
        if (storeName != null) {
            queryWrapper.like("store_name", storeName);

        }
        //分类
        if (cateId != null) {
            List<YxStoreCategory> pid = yxStoreCategoryMapper.selectList(new QueryWrapper<YxStoreCategory>().eq("pid", cateId));
            List<Integer> collect = pid.stream().mapToInt(YxStoreCategory::getId).boxed().collect(Collectors.toList());
            queryWrapper.in("cate_id", collect);
        }

        if (priceId != null) {
            QueryWrapper<PriceRange> price = new QueryWrapper<>();
            price.eq("price_range_id", priceId);
            List<PriceRange> priceRanges = priceRangeMapper.selectList(price);
            queryWrapper.between("price", priceRanges.get(0).getPriceRangeMin(), priceRanges.get(0).getPriceRangeMax());

        }
        queryWrapper.eq("is_del", 0).eq("is_show", 1).eq("is_new", 1);
        return yxStoreProductMapper.selectList(queryWrapper);
    }

    @Override
    public List<YxStoreProduct> selectYxStoreProductHot() {
        QueryWrapper<YxStoreProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", 0).eq("is_show", 1).eq("is_hot", 1);
        return yxStoreProductMapper.selectList(queryWrapper);
    }

    @Override
    public List<YxStoreProduct> selectYxStoreProductBenefit() {
        QueryWrapper<YxStoreProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", 0).eq("is_show", 1).eq("is_benefit", 1);//优惠
        return yxStoreProductMapper.selectList(queryWrapper);
    }
}
